﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Sharpie.Libs;

namespace SharpieAndroid.Libs
{
	public class DialogBox : DrawableGameComponent
	{
		SpriteBatch _batch;
		Texture2D blankT, sideT, cornerT, centerT, closeT;

		public float Opacity = 0F;
		public float Scale = 0F;
		double radians = MathHelper.ToRadians(20F);
		double minradians = MathHelper.ToRadians(20F);

		public int Width { get; set; }
		public int Height { get; set; }
		new public bool Visible
		{
			get
			{
				if (Opacity == 0F & !_visible)
				{
					return false;
				}
				else return true;
			}
		}

		bool _visible = false;
		Button closebt;

		public DialogBox(Game game)
			: base(game)
		{
			Width = 150;
			Height = 100;
			Initialize();
		}

		public override void Initialize()
		{
			base.Initialize();
			closebt = new Button(closeT, Game);
		}

		protected override void LoadContent()
		{
			base.LoadContent();
			_batch = new SpriteBatch(GraphicsDevice);
			blankT = new Texture2D(GraphicsDevice, 1, 1);
			blankT.SetData<Color>(new Color[] { Color.White });
			sideT = Game.Content.Load<Texture2D>("Images\\dialog-side");
			cornerT = Game.Content.Load<Texture2D>("Images\\dialog-corner");
			centerT = Game.Content.Load<Texture2D>("Images\\dialog-center");
			closeT = Game.Content.Load<Texture2D>("Images\\dialog-close");
		}

		public override void Update(GameTime gameTime)
		{
			base.Update(gameTime);
			if (_visible)
			{
				if (radians < MathHelper.ToRadians(90F))
					radians += MathHelper.ToRadians(5F);
				if (radians > MathHelper.ToRadians(90F))
					radians = MathHelper.ToRadians(90F);
				if (Opacity < 0.5F)
				{
					Opacity += 0.04F;
					if (Opacity > 0.5F)
						Opacity = 0.5F;
				}
			}
			else
			{
				if (radians > minradians)
					radians -= MathHelper.ToRadians(5F);
				if (radians < minradians)
					radians = minradians;
				if (Opacity > 0F)
				{
					Opacity -= 0.04F;
					if (Opacity < 0F)
						Opacity = 0F;
				}
			}
			Scale = (float)Math.Sin(radians);

			closebt.Rectangle = new Rectangle(
				(int)((GetRectangle().X - 20) + (GetRectangle().Width / 2 - (GetRectangle().Width / 2) * Scale)),
				(int)((GetRectangle().Y - 20) + (GetRectangle().Height / 2 - (GetRectangle().Height / 2) * Scale)),
				(int)(40 * Scale),
				(int)(40 * Scale));
			closebt.Update(gameTime);
			if (closebt.Clicked)
				Hide();
		}

		public override void Draw(GameTime gameTime)
		{
			base.Draw(gameTime);
			_batch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend);
			_batch.Draw(blankT, GraphicsDevice.Viewport.Bounds, Color.Black * Opacity);
			_batch.Draw(cornerT, new Vector2(GetRectangle().Center.X, GetRectangle().Center.Y), null, Color.White, 0F,
				new Vector2(GetRectangle().Width / 2, GetRectangle().Height / 2), Scale, SpriteEffects.None, 0F);
			_batch.Draw(sideT, new Vector2(GetRectangle().Center.X, GetRectangle().Center.Y), new Rectangle(0, 0, Width - cornerT.Width * 2, sideT.Height), Color.White, 0F,
				new Vector2(GetRectangle().Width / 2 - cornerT.Width, GetRectangle().Height / 2), Scale, SpriteEffects.None, 0F);
			_batch.Draw(cornerT, new Vector2(GetRectangle().Center.X, GetRectangle().Center.Y), null, Color.White, 0F,
				new Vector2(-GetRectangle().Width / 2 + cornerT.Width, GetRectangle().Height / 2), Scale, SpriteEffects.FlipHorizontally, 0F);
			_batch.Draw(sideT, new Vector2(GetRectangle().Center.X, GetRectangle().Center.Y), new Rectangle(0, 0, Height - cornerT.Width * 2, sideT.Height), Color.White, MathHelper.ToRadians(-90F),
				new Vector2(GetRectangle().Height / 2 - cornerT.Width, GetRectangle().Width / 2), Scale, SpriteEffects.None, 0F);
			_batch.Draw(centerT, new Vector2(GetRectangle().Center.X, GetRectangle().Center.Y), new Rectangle(0, 0, Width - cornerT.Width * 2, Height - sideT.Height * 2), Color.White, 0F,
				new Vector2(GetRectangle().Width / 2 - sideT.Height, GetRectangle().Height / 2 - sideT.Height), Scale, SpriteEffects.None, 0F);
			_batch.Draw(sideT, new Vector2(GetRectangle().Center.X, GetRectangle().Center.Y), new Rectangle(0, 0, Height - cornerT.Width * 2, sideT.Height), Color.White, MathHelper.ToRadians(90F),
				new Vector2(GetRectangle().Height / 2 - cornerT.Width, GetRectangle().Width / 2), Scale, SpriteEffects.None, 0F);
			_batch.Draw(cornerT, new Vector2(GetRectangle().Center.X, GetRectangle().Center.Y), null, Color.White, 0F,
				new Vector2(GetRectangle().Width / 2, -GetRectangle().Height / 2 + cornerT.Height), Scale, SpriteEffects.FlipVertically, 0F);
			_batch.Draw(sideT, new Vector2(GetRectangle().Center.X, GetRectangle().Center.Y), new Rectangle(0, 0, Width - cornerT.Width * 2, sideT.Height), Color.White, 0F,
				new Vector2(GetRectangle().Width / 2 - cornerT.Width, -GetRectangle().Height / 2 + sideT.Height), Scale, SpriteEffects.FlipVertically, 0F);
			_batch.Draw(cornerT, new Vector2(GetRectangle().Center.X, GetRectangle().Center.Y), null, Color.White, 0F,
				new Vector2(-GetRectangle().Width / 2 + cornerT.Width, -GetRectangle().Height / 2 + cornerT.Height), Scale, SpriteEffects.FlipVertically | SpriteEffects.FlipHorizontally, 0F);
			_batch.End();
			closebt.Draw(gameTime);
		}

		public Rectangle GetRectangle()
		{
			return new Rectangle(GraphicsDevice.Viewport.Bounds.Center.X - Width / 2, GraphicsDevice.Viewport.Bounds.Center.Y - Height / 2, Width, Height);
		}

		public void Show()
		{
			_visible = true;
		}

		public void Hide()
		{
			_visible = false;
		}
	}
}
